﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Longbow.Security.Cryptography;
using Bootstrap.Security;
using PetaPoco;
using Longbow.Web.Mvc;

namespace Bootstrap.DataAccess.Model
{
    /// <summary>
    /// callcountdetails表实体类
    /// </summary>

    public class InboundAbandDetails
    {
        public string ID { get; set; }
        public string Caller { get; set; }//主叫
        public string AgentNo { get; set; }//座席
        public string Callee { get; set; }//被叫
        public string InComingTime { get; set; }//进入ivr时间
        public string QueueTime { get; set; }//进入坐席组时间
        public string RingTime { get; set; }//振铃时间
        public string TalkTime { get; set; }//通话时间
        public string SessionID { get; set; }//isessionid
        public string EndTime { get; set; }//结束时间
        public string Result { get; set; }//结果
        public string CallSpan { get; set; }//呼叫时长
        public string QueueSpan { get; set; }//排队时长
        public string RingSpan { get; set; }//振铃时长
        public string TalkSpan { get; set; }//通话时长
        public string Staisfaction { get; set; }//满意度
        public string HangupReason { get; set; }//
        public string vfiled01 { get; set; } //备用字段1
        public string vfiled02 { get; set; }//备用字段2
        public string vfiled03 { get; set; }//备用字段3
        public string vfiled04 { get; set; }//备用字段4
        public string vfiled05 { get; set; }//备用字段5
        public string agent_id { get; set; }//座席号
        public string Date { get; set; }//日期
        public string BeginTime { get; set; }//开始时间



        //查询呼入明细sql
        private string _sql = "SELECT a.*,b.agent_id,left(InComingTime, 10) as Date FROM callcountdetails a "
                      + "left JOIN v_call_center_agents b on convert(a.AgentNo using utf32)=convert(b.call_center_agent_uuid using utf32)  ";
        /// <summary>
        /// 查询呼入明细
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public virtual Page<InboundAbandDetails> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string CallerID, string CalleeID, string AgentNo)
        {
            var sql = new Sql(_sql);
            if (startTime.HasValue) sql.Where("InComingTime >= @0", startTime.Value);
            if (endTime.HasValue) sql.Where("InComingTime < @0", endTime.Value.AddDays(1).AddSeconds(-1));
            //if (startTime == null && endTime == null) sql.Where("InComingTime > @0", DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod()));
            if (!string.IsNullOrEmpty(CallerID)) sql.Where("Caller like '%" + CallerID + "%'");//主叫号
            if (!string.IsNullOrEmpty(CalleeID)) sql.Where("Callee like '%" + CalleeID + "%'");//被叫号
            if (!string.IsNullOrEmpty(AgentNo)) sql.Where("agent_id like '%" + AgentNo + "%'");//座席号

            sql.OrderBy($"{po.Sort} {po.Order}");

            return DbManager.Create().Page<InboundAbandDetails>(po.PageIndex, po.Limit, sql);
        }


    }
}
